// 定义app:   name:[html, ico, cn_name, is_fix, is_desk]
var app_dic = {
    'ie': ['bing.html', '1106.ico', '浏览器', 1, 0],
    'jz_user': ['select_jz_user.html', 'jz_user.ico', '账号', 1, 0],
    'clock': ['clock.html', 'clock.ico', '万年历', 0, 0]
}


// 定义date对象的format方法
Date.prototype.format = function(format){
    var o = {
    "M+" : this.getMonth()+1, //month
    "d+" : this.getDate(),    //day
    "h+" : this.getHours(),   //hour
    "m+" : this.getMinutes(), //minute
    "s+" : this.getSeconds(), //second
    "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
    "S" : this.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
    (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,
    RegExp.$1.length==1 ? o[k] :
    ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
};

$(document).on("click", "#start_menu", function() {
    $('#start_menu').toggleClass('activate');
    $('#start_menu_box_back').toggleClass('activate');
});

$(document).on("click", "#start_menu_box_back", function() {
    $('#start_menu').click();
});

$(document).on("click", "#start_menu_box", function(e) {
    e.stopPropagation();
    //防止事件冒泡到DOM树上，也就是不触发的任何前辈元素上的事件处理函数。
});


$(document).on('click', '.app', function(){
    let name = $(this).attr('name');
    if ($('#start_menu').hasClass('activate')) {
        $('#start_menu').click();
    };
    $("#app_bar .topbar_button[name!='"+name+"'].activate").click();
    // 如果任务栏里有这个app的图标
    if ($("#app_bar .topbar_button[name='"+name+"']").length>0) {
        // 如果是打开状态
        if ($("#app_bar .topbar_button[name='"+name+"']").hasClass('activate')) {
            // 如果是激活状态，切换激活状态，隐藏app_box
            $("#app_bar .topbar_button[name='"+name+"']").removeClass('activate');
            $(".app_box[name='"+name+"']").css('display','none');
            return
        }else{
            // 如果不是激活状态但是是打开状态，切换激活状态，显示app_box
            if ($("#app_bar .topbar_button[name='"+name+"']").hasClass('open')){
                $("#app_bar .topbar_button[name='"+name+"']").addClass('activate');
                $(".app_box[name='"+name+"']").css('display','flex');
                return
            }else{
                $("#app_bar .topbar_button[name='"+name+"']").addClass('open activate');
            }
        }
        }else{
            $('#app_bar').append(`
                <div class="topbar_button open activate app" name="`+name+`" style="background-image: url('./img/`+name+`.ico');" title="`+app_dic[name][2]+`"></div>
            `);;
    };
    // 如果不是激活状态而且也不是打开状态，打开app_box
    let html = `
    <div class="app_box" name="`+name+`">
        <div class="app_title">
            <div class="app_logo" style="background-image: url('./img/`+app_dic[name][1]+`');"></div>
            <span class="app_name">`+name+`</span>
            <button onclick="mix_app(this)">-</button>
            <button></button>
            <button onclick="close_app(this)">×</button>
        </div>
        <div class="app_html">
        </div>
    </div>
    `
    $('body').append(html);
    $(".app_box[name='"+name+"'] .app_html").load("./app/"+app_dic[name][0]);
})

function close_app(obj){
    let name = $(obj).parent().children()[1].innerText;
    $(".app_box[name='"+name+"']").remove();
    if ($("#app_bar .topbar_button[name='"+name+"']").hasClass('fix')) {
        $("#app_bar .topbar_button[name='"+name+"']").removeClass('open activate');
        return
    } else {
        $("#app_bar .topbar_button[name='"+name+"']").remove();
        return
    }
}

function mix_app(obj){
    let name = $(obj).parent().children()[1].innerText;
    $(".app_box[name='"+name+"']").css('display','none');
    $("#app_bar .topbar_button[name='"+name+"']").removeClass('activate');
}

function showTime(clock){
    let now_time = new Date()
    let time = now_time.format('hh:mm</br>yy/M/d')
    let arr_work = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    let week = arr_work[now_time.getDay()];
    clock.innerHTML=time + " " + week;
}

$(document).on("click", "#back_button", function() {
    // $('.app_box.activate')
    window.frames['ddd'].parent.history.back(-1);
});

// window.onload = function(){
//     var clock = document.getElementById("clock");
//     showTime(clock);
//     window.setInterval("showTime(clock)",60000);
// }

function reset_app_list(){
    for (app_name in app_dic) {
        const app_info = app_dic[app_name];
        $('#start_menu_applist').append(`<li class="app" name="`+app_name+`" style="background-image: url('./img/`+app_info[1]+`');">`+app_info[2]+`</li>`);
        if (app_info[3]==1) {
            $('#app_bar').append(`<div class="topbar_button app fix" name="`+app_name+`" style="background-image: url('./img/`+app_info[1]+`');" title="`+app_info[2]+`"></div>`);
        }
    }
}

$().ready(function(){
    var clock = document.getElementById("clock");
    showTime(clock);
    reset_app_list();
    setInterval("showTime(clock)",60000);
})